/**
 * 登陆日志主界面
 * @author LinZongxue
 * @create_date 2011-12-23
 */
define(function(){
	var LoginLogPanel = Ext.extend(Ext.grid.GridPanel, {
		border: false,
		stripeRows: true,
	    enableHdMenu: false,
	    loadMask: {msg: '正在加载数据...'},
		initComponent: function(){
			this.tbar = [
			    {text: '删除', handler: this.removeLogs, iconCls: 'icon-delete',scope: this},
			    {text: '刷新', handler: this.refresh, iconCls: 'icon-refresh',scope: this}
			];
			var store = this.store = new Ext.data.JsonStore({
			    remoteSort: true, 
			    root: 'result',
			    totalProperty: 'totalItems',
			    idProperty: 'id',
			    fields: ['userId', 'loginName', 'userName', 'departName', 'loginTime', 'ipAddress', 'browser', 'logoutTime', 'logoutType'],
			    url: 'sys/log/queryLoginLogs.do'
			});
			this.columns = [
				new Ext.grid.RowNumberer({
					renderer : function(value,metadata,record,rowIndex){
						var lastParams = store.lastOptions.params;
						var start = lastParams && lastParams.start ? lastParams.start : 0;
						return start + rowIndex + 1;
			        }
				}),
			    this.sm = new Ext.grid.CheckboxSelectionModel(),
				{header: '账号名', width: 90, dataIndex: 'loginName'},
				{header: '用户姓名', width: 80, dataIndex: 'userName'},
				{header: '所属部门', width: 100, dataIndex: 'departName'},
				{header: '登陆时间', width: 120, dataIndex: 'loginTime', renderer: this.renderTime},
				{header: '登陆IP', width: 90, dataIndex: 'ipAddress'},
				{header: '浏览器', width: 110, dataIndex: 'browser'},
				{header: '退出时间', width: 120, dataIndex: 'logoutTime', renderer: this.renderTime},
				{header: '退出类型', width: 80, dataIndex: 'logoutType', renderer: this.renderLogoutType}
			];
			this.bbar = cu.createPagingBar(this.store);
			this.on('render', this.refresh);
			LoginLogPanel.superclass.initComponent.call(this);
		},
		refresh: function(){
			this.getStore().load();
		},
		queryLogs: function(){
			var userId = this.getTopToolbar().get('userId').getValue();
			this.getStore().reload({
				params:{userId: userId}
			});
		},
		removeLogs: function(){
			var rows = this.getSelectionModel().getSelections();
			if (rows.length == 0){
				cu.hint('请选择要删除的日志');
				return;
			}
			cu.confirm('确定要删除选择的登陆日志吗？', function(){
				cu.post({
					url:'sys/log/deleteLoginLogs.do', 
					params: {ids: lu.pluck(rows, 'json.id')},
					success: function(){
						cu.hint('删除登陆日志成功');
						this.refresh();
					},
					scope: this
				});
			}, this);
		},
		renderTime: function(v){
			return v ? new Date(v).format('Y-m-d H:i:s') : '';
		},
		renderLogoutType: function(v){
			return au.getEnumItemText('sys.LoginLog$LogoutType', v);
		}
	});
	return LoginLogPanel;
});